//
//  JBWNetWorkManager.m
//  JBWDSK
//
//  Created by 崔志伟 on 2018/8/3.
//  Copyright © 2018年 崔志伟. All rights reserved.
//

#import "JBWNetWorkManager.h"
#import "JBWCommonParameterManager.h"

@implementation JBWNetWorkManager
+(void)postUrl:(NSString *)url parameters:(NSDictionary *)parameters startRequest:(JBWNetHTTPStartRequest )startRequest finishRequest:(JBWNetHTTPFinishRequest)finishRequest errorRequest:(JBWNetHTTPErrorRequest)errorRequest{
    
    //构建对象
    if (!url) {
        return;
    }
    JBWNetHTTPRequest* request = [[JBWNetHTTPRequest alloc] initWithUrlString:url];
    //网络请求方式  可以不写   LHHTTPRequest默认情况下  如果没有请求体则是GET，存在请求体则是POST
    request.requestMethod = @"POST";
    //请求超时时间
    request.requestTimeOut = 30.0f;
    //传入字典
    NSMutableDictionary * pp = [[NSMutableDictionary alloc]initWithDictionary:parameters];
    [pp setDictionary:[JBWCommonParameterManager sharedInstance].commonParameter];
    request.requestBody =  pp;
    //请求开始
    [request requestStart:startRequest];
    //开始接收数据   如果数据量大  则会多次回调此方法
    [request requestReceiveData:^(NSData *data) {
    }];
    //回调进度 currentBytes:本次接收的数据大小 alreadyReceiveBytes:已经接收的数据大小 totalBytes:数据总大小
    [request requestUpdateProgress:^(JBWNetProgressLong currentBytes, JBWNetProgressLong alreadyReceiveBytes, JBWNetProgressLong totalBytes) {
    }];
    //网络请求结束后回调的block  reslutData为总数据
    [request requestFinish:finishRequest];
    
    //网络请求错误回调
    [request requestError:errorRequest];
    //取消下载回调
    [request requestCancel:^{
    }];
    //异步请求
    [request startAsynchronous];
    
}

+(void)getUrl:(NSString *)url parameters:(NSDictionary *)parameters  startRequest:(JBWNetHTTPStartRequest )startRequest finishRequest:(JBWNetHTTPFinishRequest)finishRequest errorRequest:(JBWNetHTTPErrorRequest)errorRequest{
    
    //构建对象
    if (!url) {
        return;
    }
    NSMutableDictionary * pp = [[NSMutableDictionary alloc]initWithDictionary:parameters];
    [pp setDictionary:[JBWCommonParameterManager sharedInstance].commonParameter];
    JBWNetHTTPRequest* request = [[JBWNetHTTPRequest alloc] initWithUrlString:[self getParameters:pp url:url]];
    //网络请求方式  可以不写   LHHTTPRequest默认情况下  如果没有请求体则是GET，存在请求体则是POST
    request.requestMethod = @"GET";
    //请求超时时间
    request.requestTimeOut = 30.0f;
   
    //请求开始
    [request requestStart:startRequest];
    //网络请求结束后回调的block  reslutData为总数据
    [request requestFinish:finishRequest];
    //网络请求错误回调
    [request requestError:errorRequest];
    //取消下载回调
    [request requestCancel:^{
    }];
    //异步请求
    [request startAsynchronous];
    
}


+ (NSString *)getParameters:(NSMutableDictionary *)parameters url:(NSString *)url {
    NSMutableString *mutableUrl = [[NSMutableString alloc] initWithString:url];
    if ([parameters allKeys]) {
        [mutableUrl appendString:@"?"];
        for (id key in parameters) {
            NSString *value = [[parameters objectForKey:key] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
            [mutableUrl appendString:[NSString stringWithFormat:@"%@=%@&", key, value]];
        }
    }
    NSString *urlEnCode = [[mutableUrl substringToIndex:mutableUrl.length - 1] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    return urlEnCode;
}


@end
